import 'package:flutter/cupertino.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class MyRefresh extends StatelessWidget {
  final RefreshController refreshController;
  final Widget child;
  final VoidCallback? onRefresh;
  final VoidCallback? onLoad;
  final bool? enablePullUp;

  const MyRefresh({super.key, required this.refreshController, required this.child, this.enablePullUp, this.onRefresh, this.onLoad});

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      controller: refreshController,
      enablePullDown: onRefresh != null,
      enablePullUp: enablePullUp ?? onLoad != null,
      header: const ClassicHeader(
        refreshingText: '正在加载中',
        completeText: '刷新成功',
        idleText: '下拉可以刷新',
        releaseText: '松开立即刷新',
      ),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus? mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = const Text("上拉加载");
          } else if (mode == LoadStatus.loading) {
            body = const CupertinoActivityIndicator();
          } else if (mode == LoadStatus.failed) {
            body = const Text("加载失败！点击重试！");
          } else if (mode == LoadStatus.canLoading) {
            body = const Text("松手,加载更多!");
          } else {
            body = const Text("没有更多数据了!");
          }
          return SizedBox(
            height: 55.0,
            child: Center(child: body),
          );
        },
      ),
      onRefresh: onRefresh,
      onLoading: onLoad,
      child: child,
    );
  }
}
